In this tutorial, we will discuss those function that takes variables as arguments and perform tasks with the help of passed arguments. So let's discuss Python Functions Arguments.
Python Functions Arguments
Arguments:
Arguments are the variables' names or values passed along the function name inside the parenthesis when we call or declare a function.
Syntax of Argument in python:
def function_name(argument1, argument2): # declaring a function with 2 arguments
function body
function_name(value1 , value2) #function calling
Let’s understand it with an example:
def say(arg1,arg2):
print(arg1)
print(arg2)
h = "hello"
w = "world"
say(h,w)
#Output
hello
world
Behind the Code
In the above example, you can see that first, we have defined a function, say(arg1,arg2), and inside the function, we print out the values of arg1 and arg2. But this time, when we call the function say(), we have passed 2 variables h and w along the function say (). Always remember when you call a function, always pass the same number of variables, that many arguments have been declared. In the above example, we have declared 2 arguments arg1 and arg2, and when we call the function, we pass the same number of variables.
Default argument:
Until now, we have seen that when we define a function, we pass arguments along with it, and those arguments get their value when we call the function with the variables. We also discuss that the number of variables or values at function calling should equal the number of arguments assigned to the function definition. With the help of the default argument concept, it’s not necessary to pass the same number of variables as arguments. We assign some value to the arguments when we define the function in default arguments. So, it’s on the user whether to provide the same number of variables while calling the function or not if the user does not provide the variable during the function calling the default value assigned to the arguments, what if the user passes the variable during the function call, in that case, the values or variable provided during function calls will overwrite the default values to the arguments.
Let’s understand it with examples:
- Default argument with no variables at function calling:
def say(arg1 ="saying" , arg2 ="hello" ): # Default arguments
print(arg1 , arg2)
say() #no variables with function calling
#Output
saying hello
- Default arguments with variables at function calling:
def add(num_1 = 1, num_2 = 1 ):
return num_1 + num_2
result = add(4,7)
print(result)
#Output
11
Behind the code
In the above example 1 we have declared a function say() with 2 arguments arg1 and arg2 and also give some value to those arguments, and those values are default values to the arguments. When we call the function say (), we did not pass any value along with it and the program run error-free because arguments got their values by default. In example 2, we declared a function add () with two arguments num_1 and num_2. Like example 2, both the arguments got some default value, but in this example, when we call the function with 2 values, those values at function calling replace the default values of arguments, and we got output 11 instead of 2 .
Arbitrary Arguments:
In an Arbitrary argument, we can pass as many as arguments we want with a single argument name and asterisk (*) sign before the argument. We do not know how many variables to pass when we call a function in some function. So, when we define a function, we use the Arbitrary Arguments that can accept more than as many variables as an argument.
There are two types of Arbitrary Arguments
- *args
- **kwargs
the name args and kwargs are conventional. The main deal is the asterisk (*) mark. With argrs we are using only one (*) sign, which means that the variables passed at the function call, will be treated as tuples, and with the kwargs (**) sign, they will treat as a dictionary.
Let’s understand it with an example:
- Arbitrary Arguments with *args
def add(*args):
sum = 0
print("args type is",type(args))
for i in args:
sum+=i
print(sum)
add(1,3,4,5,6,7,8,9,10)
#Output
args type is <class 'tuple'>
53
- Arbitrary Arguments with **kwargs
def add(**kwrgs):
sum = 0
print("kwrgs type is",type(kwrgs))
for i in kwrgs.values():
sum+=i
print(sum)
add(o=1,t=3,f=5,s=7,n=9)
#Output
kwrgs type is <class 'dict'>
25